package com.green.business.controller;

import cn.hutool.core.date.DateTime;
import com.green.business.domain.Member;
import com.green.business.service.MemberService;
import com.green.business.vo.MemberVo;
import com.green.system.common.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("member")
public class MemberController {
    @Autowired
    private MemberService memberService;

//    查询全部会员
    @RequestMapping("loadAllMember")
    public Object loadAllMember(MemberVo memberVo){
        return this.memberService.queryAllMember(memberVo);
    }

//    查询当前手机号会员
    @RequestMapping("queryPhoneMember")
    public Object queryPhoneMember(String phone){
        Member member = this.memberService.queryPhoneMember(phone);
        if (member == null || member.equals("")){
            return null;
        }else {
            return member;
        }
    }

//    添加会员
    @PostMapping("addMember")
    public ResultObj addMember(Member member){
        try {
//            注册时间默认
            member.setRegistrationtime(new DateTime());
            this.memberService.saveMember(member);
            return ResultObj.ADD_SUCCESS;

        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

//    修改会员信息
    @PostMapping("updateMember")
    public ResultObj updateMember(Member member){
        try{
            this.memberService.updateMember(member);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

//    删除会员信息
    @RequestMapping("deleteMember")
    public ResultObj deleteMember(Integer id){
        try {
            this.memberService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
}
